package com.east.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.east.dto.ActivityRemarkDTO;
import com.east.entity.ActivityRemark;
import com.east.entity.User;
import com.east.mapper.ActivityRemarkMapper;
import com.east.result.Result;
import com.east.service.ActivityRemarkService;
import com.east.utils.PageData;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import jakarta.annotation.Resource;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ActivityRemarkServiceImpl implements ActivityRemarkService {
    @Resource
    private ActivityRemarkMapper activityRemarkMapper;

    @Override
    public Result add(ActivityRemarkDTO activityRemarkDTO) {
        User loginUser = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        ActivityRemark activityRemark = BeanUtil.copyProperties(activityRemarkDTO, ActivityRemark.class);
        activityRemark.setCreateBy(loginUser.getId());
        activityRemark.setCreateTime(new Date());
        return activityRemarkMapper.insert(activityRemark) > 0 ? Result.OK() : Result.FAIL("添加笔记失败");
    }

    @Override
    public Result list(Integer currentPage, Integer pageSize, Integer activityId) {
        Page<ActivityRemarkDTO> page = PageHelper.startPage(currentPage, pageSize);
        List<ActivityRemarkDTO> activityRemarkDTOList = activityRemarkMapper.selectInclueNameById(activityId);
        PageData<ActivityRemarkDTO> pageData = new PageData<>(currentPage, pageSize,
                Integer.valueOf(page.getTotal() + ""), page.getPages(), page.getResult());
        return Result.OK(pageData);
    }

    @Override
    public Result info(Integer remarkId) {
        if (remarkId == null) {
            return Result.FAIL("请传入备注id");
        }
        ActivityRemark activityRemark = activityRemarkMapper.selectByPrimaryKey(remarkId.longValue());
        return Result.OK(activityRemark);
    }

    @Override
    public Result update(ActivityRemarkDTO activityRemarkDTO) {
        ActivityRemark activityRemark = BeanUtil.copyProperties(activityRemarkDTO, ActivityRemark.class);
        User loginUser = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        activityRemark.setEditBy(loginUser.getId());
        activityRemark.setEditTime(new Date());
        return activityRemarkMapper.updateByPrimaryKeySelective(activityRemark) > 0 ? Result.OK() : Result.FAIL("更新失败");
    }

    @Override
    public Result delete(Integer id) {
        int i = activityRemarkMapper.logicalDelete(id);
        return i > 0 ? Result.OK() : Result.FAIL("删除失败");
    }
}
